package com.yash.rms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.roo.addon.web.mvc.controller.json.RooWebJson;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.yash.rms.domain.Timehrs;

@RequestMapping("/timehrses")
@Controller
@RooWebScaffold(path = "timehrses", formBackingObject = Timehrs.class)
@RooWebJson(jsonObject = Timehrs.class)
public class TimeHourEntryController {
	
	
	   @RequestMapping(method = RequestMethod.POST, produces = "text/html")
	    public String create(@Valid Timehrs timehrs, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
	        if (bindingResult.hasErrors()) {
	            populateEditForm(uiModel, timehrs);
	            return "timehrses/create";
	        }
	        uiModel.asMap().clear();
	        timehrs.persist();
	        return "redirect:/timehrses";
	    }
	   
	   @RequestMapping(value = "/{id}", produces = "text/html")
	    public String show(@PathVariable("id") Integer id, Model uiModel) {
		   
	        addDateTimeFormatPatterns(uiModel);
	        uiModel.addAttribute("timehrs", Timehrs.findTimehrs(id));
	        uiModel.addAttribute("itemId", id);
	        return "redirect:/timehrses";
	    }
	   
	   @RequestMapping(produces = "text/html")
	    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model uiModel) {
	        if (page != null || size != null) {
	            int sizeNo = size == null ? 10 : size.intValue();
	            final int firstResult = page == null ? 0 : (page.intValue() - 1) * sizeNo;
	            uiModel.addAttribute("timehrses", Timehrs.findTimehrsEntries(firstResult, sizeNo));
	            float nrOfPages = (float) Timehrs.countTimehrses() / sizeNo;
	            uiModel.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
	        } else {
	            uiModel.addAttribute("timehrses", Timehrs.findAllTimehrses());
	        }
	        addDateTimeFormatPatterns(uiModel);
	        return "timehrses/list";
	    }
	    
	    @RequestMapping(method = RequestMethod.PUT, produces = "text/html")
	    public String update(@Valid Timehrs timehrs, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
	        if (bindingResult.hasErrors()) {
	            populateEditForm(uiModel, timehrs);
	            return "timehrses/update";
	        }
	        uiModel.asMap().clear();
	        timehrs.merge();
	        return "redirect:/timehrses/" + encodeUrlPathSegment(timehrs.getId().toString(), httpServletRequest);
	    }
	    
}
